package cn.zjj.test;

import cn.zjj.utils.ESClientUtil;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;

public class EStest {
    TransportClient client = ESClientUtil.getClient();
    //新增
    @Test
    public void testAdd(){
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pethome", "pet", "2");
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",2L);
        map.put("name","rose");
        map.put("age",23);
        map.put("sex",0);
        //用map添加进去
        System.out.println(indexRequestBuilder.setSource(map).get());

        client.close();
    }
    //获取
    @Test
    public void testGet(){
        GetRequestBuilder getRequestBuilder = client.prepareGet("pethome", "pet", "1");
        //先获取到整体
        GetResponse documentFields = getRequestBuilder.get();
        //source 才是具体数据  以map形式取出
        System.out.println(documentFields.getSourceAsMap());

        client.close();
    }
    //修改
    @Test
    public void testUpdate(){
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "pet", "1");
        //修改也是放入map
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",18L);
        map.put("name","朴彩英");
        map.put("age",23);
        map.put("sex",0);
        //放入map
        System.out.println(updateRequestBuilder.setDoc(map).get());

        client.close();
    }
    //删除
    @Test
    public void testDel(){
        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("pethome", "pet", "2");
        System.out.println(deleteRequestBuilder.get());

        client.close();
    }

    /**
     * 综合案例:
     */
    //批量添加
    @Test
    public void testPatch() throws IOException {
        BulkRequestBuilder bulk = client.prepareBulk();
        for (int i = 2; i < 50; i++) {
            IndexRequestBuilder builder = client.prepareIndex("pethome", "user", i + "").setSource(
                    XContentFactory.jsonBuilder()
                            .startObject()
                            .field("id", i - 1)
                            .field("name", "我在源码学习Java!" + i)
                            .field("age", i - 1)
                            .field("sex", i%2)
                            .endObject()
            );
            BulkResponse result = bulk.add(builder).get();
            System.out.println(result.status());
        }
        client.close();
    }
    /**
     * - 查询用户表
     * - name包含：我在源码
     * - age在1~12之间
     * - sex=1
     * - 每页大小2
     * - 从2页开始查
     * - 按照age倒序
     */

    @Test
    public void test(){
        //搜索pethome
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("pethome");
        //指定搜索类型
        searchRequestBuilder.setTypes("user");
        //QueryBuilders工具类,指定query获取bool  包含must和filter
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        //指定dsl查询
        boolQuery.must(QueryBuilders.matchQuery("name","我在"));
        //指定过滤查询
        boolQuery.filter(QueryBuilders.rangeQuery("age").gte(1).lte(12)).filter(QueryBuilders.termQuery("sex",1));
        //将bool条件放入搜索
        searchRequestBuilder.setQuery(boolQuery);

        searchRequestBuilder.setSize(2);//页条数
        searchRequestBuilder.setFrom(2);//当前页
        searchRequestBuilder.addSort("age", SortOrder.DESC);//根据age倒序排序


        SearchHit[] hits = searchRequestBuilder.get().getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }

    }

    @Test
    public void test1(){

        SearchRequestBuilder search = client.prepareSearch("pethome");
        //搜索类型
        search.setTypes("user");
        //QueryBuilders工具类,指定query获取bool  包含must和filter
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        //dsl查询
        boolQuery.must(QueryBuilders.matchQuery("name","源码"));
        //过滤查询
        boolQuery.filter(QueryBuilders.rangeQuery("age").gte(1).lte(10)).filter(QueryBuilders.termQuery("sex",0));
        //将bool条件放入搜索
        search.setQuery(boolQuery);
        //页条数
        search.setSize(1);
        //当前页
        search.setFrom(2);
        search.addSort("age", SortOrder.ASC);//根据age正序排序


        SearchHit[] hits = search.get().getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }

    }
}
